// Photo.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents a Facebook photo.
    /// </summary>
    public sealed class Photo : FacebookEntity {

        private bool _idTransformed;
        private bool _ownerTransformed;
        private bool _albumIDTransformed;
        private bool _createdDateTransformed;

        internal Photo(Hashtable photoState)
            : base(photoState) {
        }

        /// <summary>
        /// The unique ID of the album containing this photo.
        /// </summary>
        public string AlbumID {
            get {
                if (_albumIDTransformed == false) {
                    return TransformAlbumID();
                }
                return (string)this["aid"];
            }
        }

        /// <summary>
        /// The caption of this photo.
        /// </summary>
        public string Caption {
            get {
                return GetState<string>("caption") ?? String.Empty;
            }
        }

        /// <summary>
        /// The date this photo was created.
        /// </summary>
        public DateTime CreatedDate {
            get {
                if (_createdDateTransformed == false) {
                    return TransformCreatedDate();
                }
                return (DateTime)this["created"];
            }
        }

        /// <summary>
        /// The unique ID of the photo.
        /// </summary>
        public string ID {
            get {
                if (_idTransformed == false) {
                    return TransformID();
                }
                return (string)this["pid"];
            }
        }

        /// <summary>
        /// The url of the image.
        /// </summary>
        public string ImageUrl {
            get {
                return GetState<string>("src") ?? String.Empty;
            }
        }

        /// <summary>
        /// The url of the large version of the image.
        /// </summary>
        public string LargeImageUrl {
            get {
                return GetState<string>("src_big") ?? String.Empty;
            }
        }

        /// <summary>
        /// The url associated with the photo.
        /// </summary>
        public string LinkUrl {
            get {
                return GetState<string>("link") ?? String.Empty;
            }
        }

        /// <summary>
        /// The unique ID of the user who owns this photo.
        /// </summary>
        public string OwnerID {
            get {
                if (_ownerTransformed == false) {
                    return TransformOwner();
                }
                return (string)this["owner"];
            }
        }

        /// <summary>
        /// The url of the small version of the image.
        /// </summary>
        public string SmallImageUrl {
            get {
                return GetState<string>("src_small") ?? String.Empty;
            }
        }

        private string TransformAlbumID() {
            Debug.Assert(_albumIDTransformed == false);

            string id = FacebookResponse.CoerceString(this["aid"]);

            this["aid"] = id;
            _albumIDTransformed = true;

            return id;
        }

        private DateTime TransformCreatedDate() {
            Debug.Assert(_createdDateTransformed == false);

            DateTime createdDate = FacebookResponse.CoerceDateTime(this["created"]);

            this["created"] = createdDate;
            _createdDateTransformed = true;

            return createdDate;
        }

        private string TransformID() {
            Debug.Assert(_idTransformed == false);

            string id = FacebookResponse.CoerceString(this["pid"]);

            this["pid"] = id;
            _idTransformed = true;

            return id;
        }

        private string TransformOwner() {
            Debug.Assert(_ownerTransformed == false);

            string id = FacebookResponse.CoerceString(this["owner"]);

            this["owner"] = id;
            _ownerTransformed = true;

            return id;
        }
    }
}
